package com.fafa.pos.util;

import com.fafa.pos.pojo.Config;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZhouBing on 2017/7/21.
 * 请求web service的工具类
 */
public class WebServiceUtil {


    /**
     * 请求pda的webservice
     * @param config 环境配置:Ip
     * @param methodName webservice 的 方法名
     * @param parameters 参数
     * @param timeout  超时
     * @return 响应SoapObject
     */
    public static SoapObject getResponse(final Config config,final String methodName, final Map<String,Object> parameters, final int timeout){

        final SoapObject[] response = new SoapObject[1];


       Thread td =  new Thread(new Runnable() {
            @Override
            public void run() {
                String namespace = "http://fafa.com.cn/";  //命名空间
                String transUrl = "http://192.168.1.240:9191/SodxV2.Web.Interface/WebService.asmx";
                if(config!=null&& config.getProperties().get("serverIp")!=null&&!"".equals(config.getProperties().get("serverIp").trim())&&config.getProperties().get("webService")!=null&&!"".equals(config.getProperties().get("webService").trim())){
                    transUrl = "http://"+config.getProperties().get("serverIp").trim()+"/"+config.getProperties().get("webService").trim()+"/WebService.asmx";
                }

                String method = methodName;
                //注意版本使用，这个需要跟后台询问或者从wsdl文档或者服务说明中查看
                int envolopeVersion = SoapEnvelope.VER12;
                //可能是namspace+method拼接
                //String soapAction = "http://WebXml.com.cn/getSupportCity";
                SoapObject request = new SoapObject(namespace, method);
                //参数一定注意要有序，尽管是addProperty（），不要当作HttpUrl可以使用LinkedHashMap封装

                //参数设置
                if(parameters!=null){
                    Set<String> keys = parameters.keySet();
                    for (String key:keys) {
                        request.addProperty(key,parameters.get(key));
                    }
                }

                //request.addProperty("byProvinceName", "湖北");

                //SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(envolopeVersion);  //信封

                //设置往外发的内容
                envelope.setOutputSoapObject(request);//等价于envelope.bodyOut = request;

                // java\php 开发的web service要设为false
                envelope.dotNet = true;
                HttpTransportSE se = new HttpTransportSE(transUrl,timeout);
                try {
                    //se.call(soapAction, envelope);    //ver11，第一个参数不能为空
                    se.call(namespace+method, envelope);//envolopeVersion为ver12第一个参数可以为空，必须接口支持ver12才行


                    //接收
                    response[0] = (SoapObject) envelope.bodyIn;
                    //response的处理需要根据返回的具体情况，基本都要进行下面一步
//                    SoapObject o = (SoapObject) response[0].getProperty(0);
//                    //当前方法返回的结果为一个数组
//                    Log.e("zjy", "MainActivity.java->run(): size=" + o.getPropertyCount());
//                    for (int i = 0; i < o.getPropertyCount(); i++) {
//                        Log.e("zjy", "MainActivity.java->run(): ==" + o.getPropertyAsString(i));
//                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }catch (ClassCastException e){
                    e.printStackTrace();
                    response[0] = null;
                }
            }
        });
        td.start();
        try {
            td.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return response[0];
    }
}
